
function trim(val)
{
	 return val.replace(/(^\s*)|(\s*$)/g, "");  
}
 
function isMaxLen(o)
{  
	 var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";  
	 if(o.getAttribute && o.value.length>nMaxLen)
	 {      
	 	  o.value=o.value.substring(0,nMaxLen);  
	 }  
}
 
 
  
function selectAll(){
	var checkall = document.getElementById('checkall');
	var listsize = document.getElementById('listsize').value;
	for(var i=0;i<listsize;i++){
		if(checkall.checked){
			document.getElementById('checkone'+i).checked = true;
		}else{
			document.getElementById('checkone'+i).checked = false;
		}
	}
}

function judgeCheck()
{
    var pkid_string = '';
		var listsize = document.getElementById('listsize').value;
		for(var i=0;i<listsize;i++)
		{
				if(document.getElementById('checkone'+i).checked)
				{
					pkid_string += document.getElementById('checkone'+i).value+'|';
				}
		}
    return pkid_string;
}

  
function showAjaxContent(val)
{
   var data = Math.round(Math.random()* 10000);   
   var title ="";
   var gURL="";   
   if(val=='p')
   {
     title="选择支付方式";     
     gURL = "/program/admin/order/ajaxShowPayment.jsp?height=400&width=600&data="+data;
   } 
   if(val=='s')
   {
     title="选择配送方式";     
     gURL = "/program/admin/order/ajaxShowShipping.jsp?height=400&width=600&data="+data;
   }
   if(val=='g')
   {
     title="选择商品";
     var g_cat_id = document.getElementById('g_cat_id').value;
     var g_brand_id = document.getElementById('g_brand_id').value;
     var g_keyword = trim(document.getElementById('g_keyword').value);
     g_keyword = encodeURI(encodeURI(g_keyword));      
		 if(g_cat_id=='' && g_brand_id=='' && g_keyword=='')
		 {
		      alert("至少选择或输入一种查询条件！");
		      return;   
		 }     
     var param ="&cat_id="+g_cat_id+"&brand_id="+g_brand_id+"&info="+g_keyword+"&data="+data;     
     gURL = "/program/admin/order/showMoreGoods.jsp?height=400&width=800"+param;
   }  
   
   TB_show(title,gURL,false);
}

function setPack(val,id)
{
    var  all_goods_num = document.getElementById('all_goods_num').value;
	if(all_goods_num=='' || all_goods_num=='0')
	{
	   alert('请先添加商品！');
	   val.checked=false;
	   return;
	}
	
	document.getElementById('pack_id').value = id;
	document.getElementById('pack_fee').value = val.value;
	var all_pack_fee =  Number(all_goods_num)*val.value;
	document.getElementById('packshow').innerHTML ='<font color=#FF8000>(包装费用:'+all_pack_fee+'元)</font>';          
         
}

function setShippingfee()
{
    
	var province = document.getElementById("province").value;
	if(province=="")
	{
	   alert("请先选择地区");
	   return;   
	}
	var  all_goods_num = document.getElementById('all_goods_num').value;
	if(all_goods_num=='' || all_goods_num=='0')
	{
	   alert('请先添加商品！');
	   return;
	}
	var total_price = document.getElementById('total_price').value;// all goods price
	var all_goods_weight = document.getElementById("all_goods_weight").value;
	var data = Math.round(Math.random()* 10000);   
	var title="选择配送方式";     
    var gURL = "/program/admin/order/ajaxShowShipping.jsp?height=400&width=600&province="+province+"&all_goods_weight="+all_goods_weight+"&total_price="+total_price+"&all_goods_num="+all_goods_num+"&data="+data;
    TB_show(title,gURL,false);

}
  
function subForm()
{
	
	if(document.getElementById("order_source").value=="")
	{
	  alert("请选择订单来源！");
	  document.getElementById('order_source').focus();
	  return;
	}
   if(document.getElementById("pay_id").value=="")
	{
	  alert("请选择支付方式！");
	  document.getElementById('zhifu').focus();
	  return;
	}
   
  
  if(trim(document.getElementById("consignee").value)=="")
	{
	  alert("请输入收货人！");
	  document.getElementById('consignee').focus();
	  return;
	}
  
  var email= document.getElementById("email").value;  
  
  if(trim(email)=="")
	{
	  alert("请输入电子邮件！");
	  document.getElementById('email').focus();
	  return;
	}
  
  var chemEmail=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 
	
	if(!chemEmail.test(email))
	{
		alert("Email的格式不正确！");
		document.getElementById('email').focus();
		return;
	}
	
	var zip_code = document.getElementById("zip_code").value;
	if(trim(zip_code)=="")
	{
	  alert("请输入邮编！");
	  document.getElementById('zip_code').focus();
	  return;
	}

	if(!(/\d{6}/.test(zip_code))){
		alert("邮编的格式不正确！");
		document.getElementById('zip_code').focus();
		return;
	}
	
	 var area_attr_str = "";
		if(document.getElementById("province").value!=""){
			area_attr_str += document.getElementById("province").value+"|";
			if(document.getElementById("eparchy_code").value!=""){
				area_attr_str += document.getElementById("eparchy_code").value+"|";
				if(document.getElementById("city_code").value!=""){
					area_attr_str += document.getElementById("city_code").value+"|";
				}
			}
		}
   if(area_attr_str=="")
   {
		  alert("请选择地区");
		  document.getElementById('province').focus();
		  return;   
   }  
   document.getElementById("area_attr").value=area_attr_str;
   
   //alert(document.getElementById("area_attr").value);
   
   if(trim(document.getElementById("address").value)=="")
	{
	  alert("请输入详细地址！");
	  document.getElementById('address').focus();
	  return;
	}
  


	var cellphone = document.getElementById("cellphone").value;

	if(trim(cellphone)=="")
	{
	  alert("请输入手机号！");
	  document.getElementById('cellphone').focus();
	  return;
	}

	var chemCallphone = /^0{0,1}(13[0-9]|15[3-9]|15[0-2]|18[0-9])[0-9]{8}$/;
	
	if(!chemCallphone.test(cellphone)){
		alert("手机号码格式不正确！");
		return;
	}
	
	var tel = document.getElementById("tel").value;

	if(trim(tel)=="")
	{
	  alert("请输入电话！");
	  return;
	}

	var chemTel = /^0(([1-9]\d)|([3-9]\d{2}))\d{8}$/;

	if(!chemTel.test(tel)){
		alert("电话格式输入不正确！");
		document.getElementById('cellphone').focus();
		return;
	}
	
	var tel = document.getElementById("tel").value;

	if(trim(tel)=="")
	{
	  alert("请输入电话！");
	  document.getElementById('tel').focus();
	  return;
	}

	var chemTel = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;

	if(!chemTel.test(tel)){
		alert("电话格式输入不正确！");
	    document.getElementById('tel').focus();
		return;
	}
  
	if(trim(document.getElementById("sign_building").value)=="")
	{
	  alert("请输入标志性建筑！");
	  document.getElementById('sign_building').focus();
	  return;
	}
	
   if(trim(document.getElementById("best_time").value)=="")
	{
	  alert("请输入送货时间！");
	  document.getElementById('best_time').focus();
	  return;
	}

	
    if(document.getElementById("all_order_detail").value=="")
	{
	  alert("请选择商品！");
	  document.getElementById('all_order_detail').focus();
	  return;
	}
	var invoice_content=document.getElementById("invoice_content").value;
	if(document.getElementById("invoice_radio2").checked)
	{   
		
		
			var invoice_top = document.getElementById("invoice_top");
			if(invoice_top.value=='')
			{ 
				alert("请填写发票抬头！");
				document.getElementById('invoice_top').focus();
				return;
			}
		
			if(invoice_content=='')
			{
				  alert("请选择发票内容！");
				  document.getElementById('invoice_content').focus();
				  return;
			
			}
			
	   
	}
	if(document.getElementById("shipping_id").value=="")
	{
	  alert("请选择配送方式！");
	  document.getElementById('peisong').focus();
	  return;
	}

    document.addForm.submit();	

}



function setinvoice(val1){
  
  if(val1=='1')
  {
     document.getElementById('if_need_invoice').value ='1';
	 document.getElementById('invoice_table').style.display = 'none';	
     //document.getElementById('invoice_table_0').style.display = 'none';	
  }
  
  if(val1=='2')
  {
     var  all_order_detail = document.getElementById('all_order_detail').value;
     if(all_order_detail=='')
	 {
	   alert('请先添加商品！');
	   document.getElementById("invoice_radio2").checked=false;
	   document.getElementById("invoice_radio1").checked=true;
	   return;
	 }
	 document.getElementById('if_need_invoice').value ='0';
	 document.getElementById('invoice_table').style.display = ''; 	 
  }

}

 


//------------------------------------------- update

function showAjaxAttr(goods_id,i)
{
     var data = Math.round(Math.random()* 10000);   
     var title ="";
     var gURL="";   
     title="选择支付方式";     
     gURL = "/program/admin/order/ajaxShowCatAtrr.jsp?goods_id="+goods_id+"&position="+i+"&height=400&width=600&data="+data;
     TB_show(title,gURL,false);
}	


function setGoodsAttr(val)
{
  //------  goods attr validate
	var attrSize = document.getElementById('attrsize').value;
	var attr_value='';
	for( var j = 0; j < attrSize; j++ )
	{ 
		var item;
		var attrStr ='';
		var attrId = document.getElementById('attr_id'+j).value;
		var attrName = document.getElementById('attr_name'+j).value;
		var conType = document.getElementById('con_type' + j).value;
		var defaultTag = document.getElementById('default_tag' + j).value;
		
		// select
		if(conType == '0')
		{ 
		  attrStr= document.getElementById('attr_value'+j).value;
			if( attrStr == '' && defaultTag=='0')
			{
				alert('请选择' + attrName+'！'  );
 
				return;
			}  
		}
	 
	  // input
		if(conType == '1')
		{
				attrStr = document.getElementById('attr_value'+j).value; 
				if( attrStr == '' && defaultTag=='0')
				{
					alert('请输入' + attrName+'！');
 
					//document.getElementById('attr_id'+j).focus();
					return;
				}  
		}
		
		//radio
		if( conType == '2')
		{
			var valueSize = document.getElementById('valuesize'+j).value;
			for( var r = 0; r < valueSize; r++ ){
				 if( document.getElementById('attr_value'+j+'_'+r).checked){
					attrStr =  document.getElementById('attr_value'+j+'_'+r).value; 
				 } 
			}  
			if( attrStr == '' && defaultTag=='0')
			{
				alert('请选择' + attrName+'！' );
				return;
			}  
		}
		if(attrStr != ''){
			item = attrId+"|"+attrStr;
			attr_value += item +',';
		}
	}
  var rdata = Math.round(Math.random()* 10000);
  var udata ='attr_value='+attr_value+'&rdata='+rdata; 
  jQuery.ajax({
		   type: 'POST',
		   url: '/program/admin/order/ajaxCatAttr.jsp',
		   data: udata,
           async:false,									   
		   success: function(msg){
                document.getElementById('g_attr_id_'+val).innerHTML = msg;
                document.getElementById('goods_attr_'+val).value = attr_value;
                TB_remove();		   
		   }
   }); 
           
}  

function subUpdateForm()
{
	
	if(document.getElementById("order_source").value=="")
	{
	  alert("请选择订单来源！");
	  document.getElementById('order_source').focus();
	  return;
	}
    if(document.getElementById("pay_id").value=="")
	{
	  alert("请选择支付方式！");
	  document.getElementById('zhifu').focus();
	  return;
	}
   if(document.getElementById("shipping_id").value=="")
	{
	  alert("请选择配送方式！");
	  document.getElementById('peisong').focus();
	  return;
	}
  
   if(trim(document.getElementById("consignee").value)=="")
	{
	  alert("请输入收货人！");
	  document.getElementById('consignee').focus();
	  return;
	}
  
  var email= document.getElementById("email").value;  
  
  if(trim(email)=="")
	{
	  alert("请输入电子邮件！");
	  document.getElementById('email').focus();
	  return;
	}
  
  var chemEmail=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 
	
	if(!chemEmail.test(email))
	{
		alert("Email的格式不正确！");
		document.getElementById('email').focus();
		return;
	}
	
	var zip_code = document.getElementById("zip_code").value;
	if(trim(zip_code)=="")
	{
	  alert("请输入邮编！");
	  document.getElementById('zip_code').focus();
	  return;
	}

	if(zip_code.length!=6){
		alert("邮编的格式不正确！");
		document.getElementById('zip_code').focus();
		return;
	}
	
	 var area_attr_str = "";
		if(document.getElementById("province").value!=""){
			area_attr_str += document.getElementById("province").value+"|";
			if(document.getElementById("eparchy_code").value!=""){
				area_attr_str += document.getElementById("eparchy_code").value+"|";
				if(document.getElementById("city_code").value!=""){
					area_attr_str += document.getElementById("city_code").value+"|";
				}
			}
		}
   if(area_attr_str=="")
   {
		  alert("请选择地区");
		  document.getElementById('province').focus();
		  return;   
   }  
   document.getElementById("area_attr").value=area_attr_str;
   
   //alert(document.getElementById("area_attr").value);
   
   if(trim(document.getElementById("address").value)=="")
	{
	  alert("请输入详细地址！");
	  document.getElementById('address').focus();
	  return;
	}
  


	var cellphone = document.getElementById("cellphone").value;

	if(trim(cellphone)=="")
	{
	  alert("请输入手机号！");
	  document.getElementById('cellphone').focus();
	  return;
	}

	var chemCallphone = /^0{0,1}(13[0-9]|15[3-9]|15[0-2]|18[0-9])[0-9]{8}$/;
	
	if(!chemCallphone.test(cellphone)){
		alert("手机号码格式不正确！");
		document.getElementById('cellphone').focus();
		return;
	}
	
	var tel = document.getElementById("tel").value;

	if(trim(tel)=="")
	{
	  alert("请输入电话！");
	  document.getElementById('tel').focus();
	  return;
	}

	var chemTel = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;

	if(!chemTel.test(tel)){
		alert("电话格式输入不正确！");
		document.getElementById('tel').focus();
		return;
	}
  
	if(trim(document.getElementById("sign_building").value)=="")
	{
	  alert("请输入标志性建筑！");
	  document.getElementById('sign_building').focus();
	  return;
	}
	
    if(trim(document.getElementById("best_time").value)=="")
	{
	  alert("请输入送货时间！");
	  document.getElementById('best_time').focus();
	  return;
	}
	
	if(document.getElementById("invoice_radio2").checked)
	{   
	
	var invoice_top = document.getElementById("invoice_top");
			if(invoice_top.value=='')
			{ 
				alert("请选择发票抬头！");
				document.getElementById('invoice_top').focus();
				return;
			}
			if(document.getElementById("invoice_content").value=='')
			{
				  alert("请选择发票内容！");
				  document.getElementById('invoice_content').focus();
				  return;
			
			}
	    
	  
	}
	if(trim(document.getElementById("pack_fee").value)=='')
	{
		  alert("请输入包装费用！");
		  document.getElementById('pack_fee').focus();
		  return;
	}
	if(trim(document.getElementById("tax_invoice").value)=='')
	{
		  alert("请输入发票税额！");
		  document.getElementById('tax_invoice').focus();
		  return;
	}
	if(trim(document.getElementById("shipping_fee").value)=='')
	{
		  alert("请输入运输费用！");
		  document.getElementById('shipping_fee').focus();
		  return;
	}
	
	//-----商品拼串
	//goods_id+'[@]'+cust_id+'[@]'+goods_name+'[@]'+attr_value+' [@]'+goods_num+'[@]'+sale_price+'[#]';
	var goods_size = document.getElementById('goods_size').value;
	var  order_detail_str='';
	for(var i=0;i<goods_size;i++)
	{
	   var goods_id= document.getElementById('goods_id_'+i).value;
	   var cust_id= document.getElementById('cust_id_'+i).value;
	   var goods_name= document.getElementById('goods_name_'+i).value;
	   var goods_attr= document.getElementById('goods_attr_'+i).value;
	   var goods_num= document.getElementById('goods_num_'+i).value;
	   if(trim(goods_num)=='')
	   {
	     alert('请输入购买数量！');
	     document.getElementById('goods_num_'+i).focus();
		 return;
	   }
	   var shop_price= document.getElementById('shop_price_'+i).value;
	   if(trim(shop_price)=='')
	   {
	     alert('请输入商品价格！');
	     document.getElementById('shop_price_'+i).focus();
		 return;
	   }
	   
	   var one_order_detail =goods_id+"[@]"+cust_id+"[@]"+goods_name+"[@]"+goods_attr+" [@]"+goods_num+"[@]"+shop_price+"[#]";
	   order_detail_str  +=one_order_detail;
	}
	document.getElementById('all_order_detail').value =order_detail_str;

    document.addForm.submit();	

}
 
 function setUpdateInvoice(val1){
  
  if(val1=='1')//要将此订单原有的发票先删除
  {
     document.getElementById('if_need_invoice').value ='1';	 
	 document.getElementById('tax_invoice').value =0;
	 document.getElementById('invoice_table').style.display = 'none';	
    

  }
  if(val1=='2')
  {
	 document.getElementById('if_need_invoice').value ='0';
	 document.getElementById('invoice_table').style.display = ''; 
  }

}
 
function setUpdatePack(val,id)
{
    var goods_size = document.getElementById('goods_size').value;
	var  all_goods_num=0;
	for(var i=0;i<goods_size;i++)
	{
	   if(document.getElementById('goods_num_'+i).value !='')
	   {
	        all_goods_num +=Number(document.getElementById('goods_num_'+i).value);
	   }
	
	}
	document.getElementById('pack_id').value = id;
	var all_pack_fee =  (all_goods_num*val.value).toFixed(2);
	document.getElementById('pack_fee').value =all_pack_fee;
	document.getElementById('packshow').innerHTML ='<font color=#FF8000>(包装费用:'+all_pack_fee+'元)</font>';          
         
}

//限制数字全角状态输入 允许中文输入
function   clearSBC(){ 
window.clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[\uFF00-\uFFFF]/g, ' ')) 
} 

/*function showUpdateInvoice(val)
{
	   var total_price = 0;
	   var goods_size = document.getElementById('goods_size').value;
	   for(var i=0;i<goods_size;i++)
		{
		   if(document.getElementById('shop_price_'+i).value !='')
		   {
				total_price +=Number(document.getElementById('shop_price_'+i).value);
		   }
		
		}
	   var rdata = Math.round(Math.random()* 10000);
       var udata ='para_code='+val.value+'&rdata='+rdata; 
       jQuery.ajax({
				   type: 'POST',
				   url: '/program/admin/order/ajaxShowInvoice.jsp',
				   data: udata,
				   success: function(tax_invoice){
				       tax_invoice = jQuery.trim(tax_invoice);
				       var tax_fee = (Number(total_price)*tax_invoice*0.01).toFixed(2);
                       document.getElementById('s_tax_invoice').innerHTML ='<font color=#FF8000>(税额:'+tax_fee+'元)</font>';				    
                       document.getElementById('tax_invoice').value = tax_fee;  
				   }
       }); 
	   var sel_text = val.options[val.selectedIndex].text;
	   if(sel_text=='请选择')
	   {
	     document.getElementById('invoice_table_0').style.display='none';
		 
	   }
	   else if(sel_text=='普通发票')
	   {
	      document.getElementById('invoice_table_0').style.display='';
		  
	   }
} 
 */
	   
	   




